package org.opens.lambda;

import lombok.Data;
import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.util.Lists;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Data
public class UserEntity implements Comparable<UserEntity> {

    private Integer id;

    private String name;

    private Integer age;

    private String sex;

    private Date startDate;

    public UserEntity() {
    }

    public UserEntity(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public UserEntity(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    /**
     * 功能:
     *      随机产生一个List<UserEntity>
     * @param number    随机的个数
     * @return          list
     */
    public static List<UserEntity> getList(int number) {
        List<UserEntity> list = Lists.newArrayList();
        Random random = new Random();
        for(int i = 0; i < number; i++) {
            UserEntity userEntity = new UserEntity(random.nextInt(), RandomStringUtils.randomAlphanumeric(5), random.nextInt(60) + 20, random.nextBoolean() ? "男" : "女");
            userEntity.setStartDate(new Date());
            list.add(userEntity);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }

    public static UserEntity getRandomUser() {
        Random random = new Random();
        return new UserEntity(random.nextInt(), RandomStringUtils.randomAlphanumeric(5), random.nextInt(60) + 20, random.nextBoolean() ? "男" : "女");
    }

    @Override
    public int compareTo(UserEntity o) {
        if (o == null) {
            return 1;
        }
        if (o.getStartDate() == null) {
            return 1;
        }

        if (this.getStartDate() == null) {
            return -1;
        }
        return o.getStartDate().compareTo(this.getStartDate());
    }
}
